import utils from './utils'
import EventTarget from './Cevent'

export default class Cnode extends EventTarget{
    constructor(){
		super()
		utils.define(this, {children: []}, {readonly: true})
	}
    removeChild(child){
		let i = this.children.length;
		for (; i--;) {
			if(child === undefined){
				this.children.splice(i, 1)
			}else if (this.children[i] === child) {
				this.children.splice(i, 1)
				break;
			}
		}
	}
	find(selector){
		let i = 0, len = this.children.length, res = []
		if(/^#/.test(selector)){
			for (; i < len; i++) {
				if(this.children[i].id === selector.slice(1)){
					res.push(this.children[i])
					break
				}
			}
		}else if(/^\./.test(selector)){
			for (; i < len; i++) {
				if(this.children[i].className === selector.slice(1)){
					res.push(this.children[i])
					let arr = this.children[i].find(selector)
					res.push(...arr)
				}
			}
		}else if(/div|img/.test(selector)){
			for (; i < len; i++) {
				if(this.children[i].tagName === selector){
					res.push(this.children[i])
					let arr = this.children[i].find(selector)
					res.push(...arr)
				}
			}
		}
		return res
	}
}